package scjhgl;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import scjhgl.Scdd;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

public class XsddxxglController extends Controller {

	
	public void index() {
		renderFreeMarker("/scjhgl/xsddxxgl.html");	
	}
	
	public void getMenuList() {
		List dataList = Db.find("select * from ac_act");
		Map<String, Object> resp = new HashMap<String, Object>();
		resp.put("code", 0);
		resp.put("content", dataList);
		renderJson(resp);
	}
	
	public void console() {
		// renderFreeMarker("console.html");
		renderFreeMarker("console.html");
	}

 
	public void Tbody_list1(){
		Page<?> dataPage= Db.paginate(getParaToInt("pageNumber", 1), 3, "SELECT *","FROM scjhgl_scdd where xsddzt=0  ORDER BY fhrq asc");   //未排产
		renderJson(dataPage);
	}
	
	public void add() { // 增加新的销售订单
		getModel(Scdd.class,"scjhgl_scdd").save();
		redirect("/#xsddxxgl");
	}	
	
	public void delete() { // 删除销售订单
		Scdd.dao.deleteById(getPara("id"));
		renderText("success");
	}
	public void u() { // 改1
		Scdd scdd=getModel(Scdd.class,"scjhgl_scdd");
		scdd.update();
		redirect("/#xsddxxgl");

	}
	
	
public void PC1(){      					 //生成产品订单和自制品订单
		
		String idlist =getPara("idlist");
		System.out.println(idlist);
		String ids[]=idlist.split(",");
		
		for (int j = 0; j < ids.length; j++) {
			
			
			int id=Integer.valueOf(ids[j]);
			System.out.println(id);
			Date date=new Date();//获取时间
			SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd ");//转换格式
			String dt=df.format(date);
		
			String dt4=null;
			String dt5=dt.substring(2, 4);
			String dt6=dt.substring(5, 7);
			String dt7=dt.substring(8, 10);
			dt4=dt5+dt6+dt7+"000";
		
			
			int nn=Integer.parseInt(dt4);
			Scdd column3 = Scdd.dao.findByIdLoadColumns(id,"xsddh,chbm,ddsl ,khjc, fhrq"); 
			
			//Long xsddh1 =column3.getLong("xsddh");
			//int xsddzt=column3.get("xsddzt");
			List<Scdd> ccpp=Scdd.dao.find("select * from scjhgl_scdd where xsddzt='1' ");
			int ttt=ccpp.size();//先获得原有数据的条数
			Scdd.dao.findById(id).set("xsddzt","1").set("scddh",nn+ttt+1).update();     			 //-----------------------------------更改订单状态
			
		
					
				Zzpscdd zz = new Zzpscdd();
				Scdd column2 = Scdd.dao.findByIdLoadColumns(id,"xsddh,chbm,ddsl ,khjc, fhrq");  	//根据传入的id，搜寻所在列 ,存货编码 即为 母件编码
					Long chbm =column2.getLong("chbm");    //存货编码
					Long xsddh =column2.getLong("xsddh");    //销售订单号

					
					List<Bom> bom = Bom.dao.find("select * from scjhgl_bom where zjsx = '自制'&& mjbh="+chbm);   //查找boom表中对应  母件编号(存货编码)的自制信息
					List<Kcxx> kcxx = Kcxx.dao.find("select  scjhgl_bom.zjbm,kcl from scjhgl_kcxx,scjhgl_bom where scjhgl_kcxx.zjbm = scjhgl_bom.zjbm");      //获取 库存信息表 中的 库存量(通过子件编码 )

					int dpcs,scsl;   //需要计算得出
					int xql,jbyl;    //bom表
					int kcl;
					int ypcs=0; //已排产数 默认为0 ,累加
					int sl = column2.getInt("ddsl");    //订单数量 _销售订单表
					
					for (int i = 0; i <bom.size(); i++) {            //每个id对应的多个 自制品
						/*Date date=new Date();//获取时间
						SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd ");//转换格式
						String dt=df.format(date);*/
					
						String dt3=null;
						String dt0=dt.substring(2, 4);
						String dt1=dt.substring(5, 7);
						String dt2=dt.substring(8, 10);
						dt3=dt0+dt1+dt2+"000";
						int n=Integer.parseInt(dt3);
						System.out.println("###@@"+dt3);
						

						kcl = kcxx.get(i).getInt("kcl");  //库存量 _Kcxx表
						jbyl = bom.get(i).getInt("jbyl");  //基本用量_bom表
						
						xql= sl * jbyl; 		//需求量 =订单数量 * 基本用量
						if(xql - kcl<0){		//生产数量
							scsl = xql;  		
						}else {
							scsl=xql-kcl;
						}
						dpcs = scsl - ypcs; 	 //待排产数
						
						List<Zzpscdd> ccpp1=Zzpscdd.dao.find("select * from scjhgl_zzpscdd  ");
						int tt=ccpp1.size();//先获得原有数据的条数
						
						
						zz.set("ddh", column2.getLong("xsddh"))  // 订单号 _销售订单表  一 对 多
						  .set("scddh", n+1+tt)
						  .set("zjbm", bom.get(i).get("zjbm"))   //子件编码 _bom表
						  .set("zjmc", bom.get(i).get("zjmc"))  //子件名称_bom表
						  .set("zjgg", bom.get(i).get("zjgg"))  //子件规格_bom表
						  .set("jl", bom.get(i).get("jl"))   //计量_bom表
						  .set("xql", xql)     
						  .set("dpcs",dpcs)
						  .set("ypcs", ypcs)
						  .set("scsl", scsl);
							
						zz.save();				 //-----------------------------------------------------------------------向自制品生产订单表插入数据
					}	 
		}
		renderText("success");
	}
      
	public void search1() throws UnsupportedEncodingException{     		// search for 未排产
		String require =getPara("require","");  	//得到result的值  默认值问"" 
		Page<?> dataPage=Db.paginate(getParaToInt("pageNumer",1), 3, "select *", "from scjhgl_scdd where xsddzt=0 && xsddh like '%"+require+"%' ORDER BY  fhrq asc");
		if(dataPage.getList().isEmpty()){               //第一次搜索为空
				dataPage=Db.paginate(getParaToInt("pageNumer",1), 3, "select *", "from scjhgl_scdd where xsddzt=0 && khjc like '%"+require+"%' ORDER BY fhrq asc");
					if(dataPage.getList().isEmpty()){          //第二次搜索为空
						dataPage=Db.paginate(getParaToInt("pageNumer",1), 3, "select *", "from scjhgl_scdd where xsddzt=0 && chbm like '%"+require+"%' ORDER BY fhrq asc");
						if(dataPage.getList().isEmpty()){         //如果第三个条件也为空
							dataPage=Db.paginate(getParaToInt("pageNumer",1), 3, "select *", "from scjhgl_scdd xsddzt=0 ORDER BY fhrq asc");
						}  
					}
				}              
		renderJson(dataPage);
	}	
	
	
	public void zdsc(){  //自动生成生产订单
		System.out.println("aaaaaaaaaaaaaaaaaa");
		List<Scdd> zd=Scdd.dao.find("select * from scjhgl_scdd" );   //从存货编码判断
		for (int j = 0; j < zd.size(); j++) {		
			zd.get(j).set("xsddzt", "1").update();
		}
		
		
		redirect("#xsddxxgl");
    }	
	
	
	
}
